package redecolaborativa.client;

import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

public class RequisitaItensEmprestados extends Composite {
	private FachadaAsync fachada;
	private String idSessao;
	private AbsolutePanel absolutePanel;
	
	public RequisitaItensEmprestados(final FachadaAsync fachada, final String idSessao) {
		this.fachada = fachada;
		this.idSessao = idSessao;
		
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("Meus itens emprestados");
		cptnpnlNewPanel.setStyleName("caption-panel");
		initWidget(cptnpnlNewPanel);
		cptnpnlNewPanel.setSize("650px", "424px");
		
		ScrollPanel scrollPanel = new ScrollPanel();
		cptnpnlNewPanel.setContentWidget(scrollPanel);
		scrollPanel.setSize("651px", "401px");
		
		absolutePanel = new AbsolutePanel();
		scrollPanel.setWidget(absolutePanel);
		absolutePanel.setSize("100%", "100%");
		
		mostraItensEmprestados();
	}

	private void mostraItensEmprestados() {
		absolutePanel.clear();
		fachada.getItensEmprestados(idSessao, new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> result) {
				for(String str : result) {
					String nomeItem = str.split("-")[0];
					String beneficiado = str.split("-")[1];
					String tempoPrevisto = str.split("-")[2];
					String dataInicial = str.split("-")[3];
					String idEmprestimo = str.split("-")[4];
					
					absolutePanel.add(new ItemEmprestado(fachada, nomeItem, beneficiado, tempoPrevisto, dataInicial, idEmprestimo, idSessao));
				}
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}
}
